
include Acc3
open Result

module Make_gui_dir(Event: sig 
    type t
    val is_online: unit -> bool 
    val iter: (t -> unit) -> unit 
end) =
struct
  include Make(Event)
  module Gui = Gtk_ui.Extendable_with ( struct
    type get_values = [ func | `Graph ]
    let not_graph = function `Graph -> false | _ -> true
    open Histo
    let combine ~p1 f1 ~p2 f2 = (
        let get_result e ~proj func =
          ( match (func, proj) with 
                `H1T f, X -> f e
              | `H1T f, Y -> 
                    raise (Gtk_ui.Incorrect_combination "Y of 1d-hist.?")
              | `H2T f, p -> Common.select1d (f e) p 
              | `Graph, _ -> raise (Gtk_ui.Incorrect_combination "graph!")
          ) in
        `H2T (fun e -> 
            let result1 = get_result e ~proj:p1 f1 
            and result2 = get_result e ~proj:p2 f2 in
            ( match (result1, result2) with 
                Result (x1,w1), Result (x2,w2) -> 
                    assert (w1 = w2); Result (x1,x2,w1)
              | List_results l1, Result (x2,w2) -> 
                  ( let l = 
                    List.map (fun (x1,w1) -> assert (w1 = w2); (x1,x2,w1)) l1
                    in List_results l)
              | Result (x1,w1), List_results l2 -> 
                  ( let l =
                    List.map (fun (x2,w2) -> assert (w1 = w2); (x1,x2,w1)) l2 
                    in List_results l)
              | List_results l1, List_results l2 -> 
                  ( let l = 
                    List.fold_left (fun acc (x1,w1) -> 
                        List.fold_left (fun acc (x2,w2) -> assert (w1 = w2);
                            (x1,x2,w1)::acc
                        ) acc l2
                    ) [] l1
                    in List_results l)
            ) (* end match *)
        ) (* end fun *)
    ) (* end 'let combine' *)
    let create info f ~a_x ~a_y =
	let g = match f with 
                `H2T g -> g 
              | _ -> (failwith "int.comb.error") 
        in
        let hist = Histo.H2.h_from_axes a_x a_y info in
        Event.iter (fun e ->
            let result = g e in
            RH2.fill_misc result hist
        );
        Histo.H2d hist
  end)
    type container' = Gui.Basic.histo_container

(*  module Make_subevent = Init_dir (struct type 'a dir = 'a Gui.Basic.dir
 *  end)*)
  module Make_subevent(IM: 
      sig type t val convert: Event.t -> t type hit end) = 
  struct
(*    include Init_dir (struct type 'a dir = 'a Gui.Basic.dir end) (IM)*)
    include Init_dir (Gui.Basic.Elt) (IM)
    let fill_list l = 
        Event.iter (fill_im_list l)
  end

  (*let directory = Gui.Basic.HD.directory*)
  let directory = Gui.Basic.make


  let list_to_directory name l : Gui.Basic.t =
      Directory.Directory ( name,
          List.map (fun (o: < get_container: Gui.Basic.t; ..>) -> o#get_container) l
      )
end
